<?php

/**
 * UserIdentity represents the data needed to identity a user.
 * It contains the authentication method that checks if the provided
 * data can identity the user.
 */
class UserIdentity extends CUserIdentity
{
    private $_id;
    public $user;
    public $userId;
	/**
	 * Authenticates a user.
	 * The example implementation makes sure if the username and password
	 * are both 'demo'.
	 * In practical applications, this should be changed to authenticate
	 * against some persistent user identity storage (e.g. database).
	 * @return boolean whether authentication succeeds.
	 */
	public function authenticate()
	{
        $username = trim($this->username);
        $user_model = Manage::model()->findUserInfo($username);
        if ($user_model === null) {
            $this->errorCode = self::ERROR_USERNAME_INVALID;
            return false;

        }elseif(md5($this->username.$user_model->pwd_hash.$this->password) !== $user_model->pwd){
            $this->errorCode=self::ERROR_PASSWORD_INVALID;
            return false;
        }else{

            $count = Manage::model()->updateAll(array('last_login_ip'=>Yii::app()->request->userHostAddress,'last_login_time'=>time()),'admin_id=:pass',array(':pass'=>$user_model->admin_id));
            Yii::app()->user->setState('userSessionTimeout', time()+Yii::app()->params['sessionTimeoutSeconds']);
            $this->errorCode=self::ERROR_NONE;
            $this->setState('admin_id',$user_model->admin_id);
            $this->setState('loginnum',$user_model->logintimes);
            $this->setState('admin_name',$user_model->admin_name);
            $this->setState('real_name',$user_model->realname);
            $this->setState('rankid',$user_model->rankid);
            $this->userId = $user_model->admin_id;
            if($count > 0){
                return !$this->errorCode;
            }
        }
	}
    public function getId(){
        return $this->userId;
    }
}